package com.trendplus.unitconverter;


import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class LengthCalculate extends Activity {
	
	Spinner lengthType=null; 
	TextView mTextView;
	
	/** Called when the activity is first created. */
	//foot:英呎
    static final String[] lengthTypeList=new String[] {
        "公分", "公尺", "公里", "吋", 
        "英呎", "碼", "英哩"
    };
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.calc_length);
	    findViews();
	    touchSpinnerS();
	    setListeners();
	}
	
	private EditText inputValue;
	private Button button_calc;
	private Button button_cleanResult;
	
	
	
	public void findViews(){
		inputValue = (EditText) findViewById(R.id.inputValue);
		button_calc = (Button) findViewById(R.id.submit);
		button_cleanResult = (Button) findViewById(R.id.cleanData);
	}
	//Listen for button clicks
    private void setListeners() {
        button_calc.setOnClickListener(calcLehgth);
        button_cleanResult.setOnClickListener(cleanEditText);
    }
	
	
	public void touchSpinnerS(){
		Spinner SpinnerS = (Spinner)findViewById(R.id.mySpinner);

        //設定功能表項目陣列，使用createFromResource()
        //ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.items, android.R.layout.simple_spinner_item);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lengthTypeList);

        //設定選單
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mTextView = (TextView) findViewById(R.id.textViewLength);
        //設定adapter
        SpinnerS.setAdapter(adapter);
        
        SpinnerS.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                mTextView.setText(lengthTypeList[arg2]);
                arg0.setVisibility(View.VISIBLE);
            }
 
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
 
        });
	}
	
	private OnClickListener calcLehgth = new OnClickListener(){
		public void onClick(View v) {
			TextView lengthType = (TextView)findViewById(R.id.textViewLength);
			EditText inputValue = (EditText)findViewById(R.id.inputValue);
			String lengthTypes = lengthType.getText().toString();
			long inputValues = Long.parseLong((inputValue.getText().toString()));
			System.out.println("ChooseType="+lengthTypes+", values="+inputValues);
			
			converterOfLength(lengthTypes,inputValues);
		}
    };
    
    
    
    public void converterOfLength(String lengthType, double value){
    	
		if(lengthType.equalsIgnoreCase("公分")){
			String cmStr = Double.toString(value);
			String mStr = Double.toString(value*0.01);
			String kmStr = Double.toString(value*0.0001);
			String inchStr = Double.toString(value*0.3937);
			String footStr = Double.toString(value*0.0328);
			String yardStr = Double.toString(value*0.0109);
			String mileStr = Double.toString(value*0.0000062);
			resultSetting(cmStr, mStr, kmStr, inchStr, footStr, yardStr, mileStr);
			
		}else if(lengthType.equalsIgnoreCase("公尺")){
			String cmStr = Double.toString (value*100);
			String mStr = Double.toString(value);
			String kmStr = Double.toString (value*0.001);
			String inchStr = Double.toString(value*39.37);
			String footStr = Double.toString(value*3.28084);
			String yardStr = Double.toString(value*1.09361);
			String mileStr = Double.toString(value*0.00062);
			resultSetting(cmStr, mStr, kmStr, inchStr, footStr, yardStr, mileStr);
			
		}else if(lengthType.equalsIgnoreCase("公里")){
			String cmStr = Double.toString (value*1000*100);
			String mStr = Double.toString(value*1000);
			String kmStr = Double.toString (value);
			String inchStr = Double.toString(value*39370);
			String footStr = Double.toString(value*3280.84);
			String yardStr = Double.toString(value*1093.61);
			String mileStr = Double.toString(value*0.62137);
			resultSetting(cmStr, mStr, kmStr, inchStr, footStr, yardStr, mileStr);
			
		}else if(lengthType.equalsIgnoreCase("吋")){
			String cmStr = Double.toString (value*2.54);
			String mStr = Double.toString(value*0.0254);
			String kmStr = Double.toString (value*0.00003);
			String inchStr = Double.toString(value);
			String footStr = Double.toString(value*0.08333);
			String yardStr = Double.toString(value*0.02778);
			String mileStr = Double.toString(value*0.00002);
			resultSetting(cmStr, mStr, kmStr, inchStr, footStr, yardStr, mileStr);
			
		}else if(lengthType.equalsIgnoreCase("英呎")){
			String cmStr = Double.toString (value*30.4801);
			String mStr = Double.toString(value*0.3048);
			String kmStr = Double.toString (value*0.00031);
			String inchStr = Double.toString(value*12);
			String footStr = Double.toString(value);
			String yardStr = Double.toString(value*0.33333);
			String mileStr = Double.toString(value*0.00019);
			resultSetting(cmStr, mStr, kmStr, inchStr, footStr, yardStr, mileStr);
			
		}else if(lengthType.equalsIgnoreCase("碼")){
			String cmStr = Double.toString (value*91.4402);
			String mStr = Double.toString(value*0.9144);
			String kmStr = Double.toString (value*0.00091);
			String inchStr = Double.toString(value*36);
			String footStr = Double.toString(value*3);
			String yardStr = Double.toString(value);
			String mileStr = Double.toString(value*0.00057);
			resultSetting(cmStr, mStr, kmStr, inchStr, footStr, yardStr, mileStr);
			
		}else if(lengthType.equalsIgnoreCase("英哩")){
			String cmStr = Double.toString (value*160935);
			String mStr = Double.toString(value*1609.35);
			String kmStr = Double.toString (value*1.60935);
			String inchStr = Double.toString(value*63360);
			String footStr = Double.toString(value*5280);
			String yardStr = Double.toString(value*1760);
			String mileStr = Double.toString(value);
			resultSetting(cmStr, mStr, kmStr, inchStr, footStr, yardStr, mileStr);
			
		}
		
	}
    
    private OnClickListener cleanEditText = new OnClickListener(){

		public void onClick(View v) {
			EditText inputValue = (EditText)findViewById(R.id.inputValue);
			inputValue.setText("");
			resultSetting("__", "__", "__","__", "__", "__", "__");
			
		}
    };
	
	
	//結果填入欄位內
	public void resultSetting(String cmStr, String mStr, String kmStr,
			String inchStr, String footStr, String yardStr, String mileStr){
		TextView cmResult = (TextView)findViewById(R.id.cmResult);
		TextView mResult = (TextView)findViewById(R.id.mResult);
		TextView kmResult = (TextView)findViewById(R.id.kmResult);
		TextView inchResult = (TextView)findViewById(R.id.inchResult);
		TextView footResult = (TextView)findViewById(R.id.footResult);
		TextView yardResult = (TextView)findViewById(R.id.yardResult);
		TextView mileResult = (TextView)findViewById(R.id.mileResult);
		
		cmResult.setText("= " +  cmStr + "公分");
		mResult.setText("= " +  mStr + "公尺");
		kmResult.setText("= " +  kmStr + "公里");
		inchResult.setText("= " +  inchStr + "吋");
		footResult.setText("= " +  footStr + "英呎");
		yardResult.setText("= " +  yardStr + "碼");
		mileResult.setText("= " +  mileStr + "英哩");
		
		
	}
	
	
	  
}
